package personal.jinhui.paprika.ui.dialogs.builders;

import personal.jinhui.paprika.service.Contact;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.RemoteException;
import android.util.Log;

import personal.jinhui.paprika.R;
import personal.jinhui.paprika.service.aidl.IRoster;

/**
 * Use this builder to build a dialog which allows you to delete a contact from
 * a specific roster.
 * 
 * @author Jean-Manuel Da Silva <dasilvj at Paprika-project dot com>
 */
public class DeleteContact extends AlertDialog.Builder {

	private static final String TAG = "Dialogs.Builders > DeleteContact";

	private IRoster mRoster;
	private Contact mContact;

	/**
	 * Constructor.
	 * 
	 * @param context
	 *            context activity.
	 * @param roster
	 *            the roster which has the contact you want to delete.
	 * @param contact
	 *            the contact to delete.
	 */
	public DeleteContact(final Context context, final IRoster roster,
			final Contact contact) {
		super(context);

		mContact = contact;
		mRoster = roster;

		setMessage(R.string.contact_sure2delete);
		DialogClickListener dl = new DialogClickListener();
		setPositiveButton(R.string.contact_yes, dl);
		setNegativeButton(R.string.contact_no, dl);
	}

	/**
	 * Event click listener.
	 */
	private class DialogClickListener implements
			DialogInterface.OnClickListener {

		/**
		 * Constructor.
		 */
		public DialogClickListener() {
		}

		@Override
		public void onClick(final DialogInterface dialog, final int which) {
			if (which == DialogInterface.BUTTON_POSITIVE) {
				try {
					mRoster.deleteContact(mContact);
				} catch (RemoteException e) {
					Log.e(TAG, e.getMessage());
				}
			}
		}
	}
}
